package com.liveinstars.quhui.business.appconfig.service.impl;

import com.liveinstars.quhui.business.appconfig.repository.facade.IAppVersionService;
import com.liveinstars.quhui.business.appconfig.repository.po.AppVersion;
import com.liveinstars.quhui.business.appconfig.service.AppConfigDomainService;
import com.onepiece.shipelves.common.utils.VersionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("appConfigService")
public class AppConfigDomainServiceImpl implements AppConfigDomainService {

    @Autowired
    IAppVersionService appVersionService;

    @Override
    public Map<String, Object> checkAppVersionWithAppCode(String version, String from, String appCode) {
        AppVersion lastVersion = appVersionService.findLastVersion(from, appCode);
        AppVersion forceUpdateVersion = appVersionService.findForceUpdateVersion(from, appCode);
        AppVersion nowVersion = appVersionService.findNowVersion(version, from, appCode);

        Map<String, Object> versionInfo = new HashMap(8);
        if (lastVersion == null) {
            lastVersion = nowVersion;
        }
        versionInfo.put("last_version", lastVersion.getVersion());
        versionInfo.put("url", lastVersion.getUrl());
        versionInfo.put("info", lastVersion.getInfo());
        //先检查[最新版本]是否需要强制更新
        versionInfo.put("force_update", lastVersion.getIsForceUpdate().equals(new Byte("1")) ? true : false);
        versionInfo.put("config_url", nowVersion != null ? nowVersion.getConfigUrl() : "");

        //再看看是否小于[需要强制更新的版本]
        if (forceUpdateVersion != null && (VersionUtil.compareVersion(version, forceUpdateVersion.getVersion()) < 0)) {
            versionInfo.put("force_update", true);
        }

        return versionInfo;
    }
}
